package tq.com.tqstudent.push;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.lib.common.utils.LogUtil;
import com.lib.common.utils.SPUtils;

import java.util.Map;

import tq.com.tqstudent.Constants;
import tq.com.tqstudent.utils.VarietyUtil;
import tq.com.tqstudent.widgets.NotificationUtil;

import static tq.com.tqstudent.ui.activity.mine.SettingActivity.PUSH_SWITCH;

/**
 * @Author rames
 * @date 2019/6/23 15:01
 */
public class AliPushReceiver extends MessageReceiver {

    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        boolean flag = !SPUtils.getInstance(context).getBoolean(Constants.IS_CHAT_ACTIVITY_PUSH); // 表示不在ChatActivity页面.推送需要显示
//        boolean flag = !VarietyUtil.isRunForeground(context);
        if(flag){ // 如果是在前台就不弹通知,在后台就弹通知
            notifyPush(context, title, summary);
        }

        LogUtil.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap+" | 是否在前台: "+flag+" | 是否显示推送: "+flag);
    }

    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        boolean flag = !SPUtils.getInstance(context).getBoolean(Constants.IS_CHAT_ACTIVITY_PUSH); // 表示不在ChatActivity页面.推送需要显示
//        boolean flag = !VarietyUtil.isRunForeground(context);
        if(flag){ // 如果是在前台就不弹通知,在后台就弹通知
            notifyPush(context, cPushMessage.getTitle(), cPushMessage.getContent());
        }
        LogUtil.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }

    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
//        LogUtil.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
//        LogUtil.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
//        LogUtil.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }

    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
//        LogUtil.e("MyMessageReceiver", "onNotificationRemoved");
    }

    private void notifyPush(Context context, String title, String content) {
        if(TextUtils.isEmpty(title)) title = "";
        if(TextUtils.isEmpty(content)) content = "";

        NotificationUtil notificationUtils =new NotificationUtil(context);
        notificationUtils.sendNotification(title, content);
    }
}
